home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-27 | 2.8 KB | 95 lines |
- //
- // lion is moving toward you.
- //
-
- import vrml.*;
- import vrml.node.*;
- import vrml.field.*;
-
- public class LionMovie extends Script{
- SFTime startLion;
- SFTime stopLion;
- SFVec3f setLionPosition;
-
- // lion is moving or stopped.
- boolean moving = false;
-
- // your position and lion position.
- float userPosition[] = new float[3];
- float lionPosition[] = new float[3];
-
- public void initialize(){
- // get the reference of the event out 'startLion'.
- startLion = (SFTime)getEventOut("startLion");
- // get the reference of the event out 'stopLion'.
- stopLion = (SFTime)getEventOut("stopLion");
- // get the reference of the event out 'startLion'.
- setLionPosition = (SFVec3f)getEventOut("setLionPosition");
-
- // initialize lion position
- lionPosition[0] = 0.0f;
- lionPosition[1] = 0.0f;
- lionPosition[2] = 0.0f;
- }
-
- public void processEvent(Event e){
- if(e.getName().equals("touched") == true){
- touched(e);
- }else if(e.getName().equals("interval") == true){
- interval();
- }else if(e.getName().equals("getUserPosition") == true){
- getUserPosition(e);
- }
- }
-
- // when you click the lion...
- void touched(Event e){
- double currentTime = ((ConstSFTime)e.getValue()).getValue();
-
- // toggle the lion state.
- moving = !moving;
-
- // start or stop the lion.
- if(true == moving){
- startLion.setValue(currentTime);
- }else{
- stopLion.setValue(currentTime);
- }
- }
-
- // when the lion is moving toward you...
- void interval(){
- if(true == moving){
- // move the lion toward you.
-
- float dx = userPosition[0] - lionPosition[0];
- float dy = userPosition[1] - lionPosition[1];
- float dz = userPosition[2] - lionPosition[2];
- float distance = (float)Math.sqrt((double)(dx * dx +
- dy * dy +
- dz * dz));
- if(distance < 3.0){
- // if the lion is near to you enough, do not move it anymore.
- return;
- }
-
- // relative movement distance is 1m.
- dx = dx / distance;
- dy = dy / distance;
- dz = dz / distance;
-
- // update lion position.
- lionPosition[0] += dx;
- lionPosition[1] += dy;
- lionPosition[2] += dz;
- setLionPosition.setValue(lionPosition);
- }
- }
-
- // watch your current position.
- void getUserPosition(Event e){
- ((ConstSFVec3f)e.getValue()).getValue(userPosition);
- }
- }
-
-